package com.wst.system.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.wst.system.security.IsAdmin;
import com.wst.system.security.entity.enm.ApiTypeEnum;
import com.wst.system.server.entity.vo.DelManyDataVo;
import com.wst.system.server.entity.vo.PageSearchVo;
import com.wst.system.util.ResultUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wst.system.config.exception.GlobalExcetion;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.wst.system.server.service.SysOpenApiService;
import com.wst.system.server.entity.SysOpenApi;
import com.wst.system.server.entity.vo.SysOpenApiVo;
import org.springframework.validation.annotation.Validated;
import java.util.List;
import com.wst.system.config.PageData;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author X.I.O
 * @since 2025-01-16
 */

@RestController
@Api(tags = {"的服务类"})
@RequestMapping("/sys-open-api")
public class SysOpenApiController  {

        @Autowired
        private SysOpenApiService sysOpenApiService;


        @IsAdmin
        @PostMapping("/saveorupdate")
        @ApiOperation(value = "的新增/更新接口",notes = "的新增接口")
        public JSONObject save(@RequestBody @Validated SysOpenApiVo sysOpenApiVo) throws GlobalExcetion{
            sysOpenApiService.saveOrUpdateSysOpenApi(sysOpenApiVo);
            return ResultUtil.successInfo("操作成功");
        }



        @IsAdmin
        @DeleteMapping("/delbyid")
        @ApiOperation(value = "的删除接口",notes = "的删除接口")
        public JSONObject delete(@RequestParam("id") Serializable id) throws GlobalExcetion{
            boolean flag = sysOpenApiService.removeById(id);
            if(flag){
                return ResultUtil.successInfo("删除成功");
            }else {
                return ResultUtil.errorInfo("删除失败");
            }
        }

        @IsAdmin
        @PostMapping("/del/batch")
        @ApiOperation(value = "的批量删除接口",notes = "的批量删除接口")
        public JSONObject deleteBatch(@RequestBody @Validated DelManyDataVo vo) throws GlobalExcetion {
            boolean flag =  sysOpenApiService.removeByIds(vo.getIds());
            if(flag){
                return ResultUtil.successInfo("删除成功");
            }else {
                return ResultUtil.errorInfo("删除失败");
            }
        }

        @GetMapping("/getall")
        @ApiOperation(value = "的获取全部接口",notes = "的获取全部接口")
        public JSONObject findAll() throws GlobalExcetion {
            List<SysOpenApi> list= sysOpenApiService.list();
            return ResultUtil.successInfo(list);
        }

        @GetMapping("/getbyid")
        @ApiOperation(value = "的获取一个接口",notes = "的获取一个接口")
        public JSONObject findOne(@RequestParam("id") Serializable id) throws GlobalExcetion {
            SysOpenApi data= sysOpenApiService.getById(id);
            return ResultUtil.successInfo(data);
        }

        @PostMapping("/page")
        @ApiOperation(value = "的分页接口",notes = "的分页接口")
        public JSONObject findPage(@RequestBody PageSearchVo vo) throws GlobalExcetion {
            IPage<SysOpenApi> data=sysOpenApiService.page(vo);
            return ResultUtil.successInfoTable(data.getTotal(),data.getRecords());
        }

        @GetMapping("/gettype")
        public JSONObject getType() throws GlobalExcetion {
            JSONArray array=new JSONArray();
            for (ApiTypeEnum api:ApiTypeEnum.values()){
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("label",api.getTypeName());
                jsonObject.put("value",api.getType());
                array.add(jsonObject);
            }
            return ResultUtil.successInfo(array);
        }
}

